﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;

namespace EFong.View.BankAccount
{
    public partial class Frm_FundsTransfer : BasicForm
    {
        private Proxy.FinanceServiceProxy _financeProxy {get{ return Proxy.FinanceServiceProxy.GetInstance();}}
        private Proxy.MasterServiceProxy _masterProxy { get { return Proxy.MasterServiceProxy.GetInstance(); } }

        public Frm_FundsTransfer()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
                {
                    this.save();
                };
        }

        protected override void LoadDatas()
        {
            List<Contract.BankAccount> bankAccountList = _masterProxy.GetAllBankAccount().ReturnValue;
            this.cmb_SourceAccount.Properties.BindingData(bankAccountList);
            this.cmb_TargetAccount.Properties.BindingData(bankAccountList);
        }

        private void save()
        {
            Contract.AccountTransfer accountTransfer = getAccountTransfer(); 

            UIOperManager.SaveData(
                    () =>
                        {
                            return true;
                        },
                    () =>
                        {
                            return _financeProxy.Transfer(accountTransfer).IsSuccess;
                        },
                    MsgResource.FundsTransfer_Complete
                );
        }

        private Contract.AccountTransfer getAccountTransfer()
        {
            return new Contract.AccountTransfer 
                {
                    ID = Guid.NewGuid(),
                    Source = (Contract.BankAccount)cmb_SourceAccount.SelectedItem,
                    TransferDate = dte_TransferDate.GetValue(DateTime.MinValue),
                    ReceiveDate = dte_ReceiveDate.GetValue(DateTime.MinValue),
                    Target = (Contract.BankAccount)cmb_TargetAccount.SelectedItem,
                    Amount = spd_Amount.GetValue(),
                    ExcehangeRate = spd_ExchangeRate.GetValue(),
                    ServiceCharge = spd_Changed.GetValue()
                };
        }
    }
}
